//
//  ZYRegisterViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/6/12.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYRegisterViewModel.h"
#import "ZYBasicHelper.h"
#import "AFNetWorkUtils.h"
#import "ZYUser.h"

@interface ZYRegisterViewModel()

@end

@implementation ZYRegisterViewModel

-(RACCommand *)codeCommand{
    if(!_codeCommand){
        _codeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @weakify(self);
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                if(NULLString(self.phone)){
                    NSString *errorInfo = @"";
                    if(self.modifyType == ZYModifyTypeEmail){
                        errorInfo = [self.language getStringByKey:LC_ERROR_EMAIL_EMPTY];
                    }else{
                        errorInfo = [self.language getStringByKey:LC_ERROR_PHONE_EMPTY];
                    }
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:errorInfo]];
                    [subscriber sendCompleted];
                    return nil;
                }
                
                if(self.modifyType == ZYModifyTypeEmail){
                    if(![ZYBasicHelper validateEmail:self.phone]){
                        [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_EMAIL]]];
                        [subscriber sendCompleted];
                        return nil;
                    }
                }else{
                    if(![ZYBasicHelper validateMobile:self.phone]){
                        [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_PHONE]]];
                        [subscriber sendCompleted];
                        return nil;
                    }
                }
                
                
                NSString *url = @"";
                NSDictionary *params;
                if(_modifyType == ZYModifyTypeEmail){
                    params = @{@"email":self.phone};
                    url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_FETCH_EMAIL_CODE_URL];
                }else{
                    params = @{@"phone":self.phone};
                    url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_FETCH_CODE_URL];
                }
                [[AFNetWorkUtils racPOSTWithURL:url params:params] subscribeNext:^(id x) {
                    RACTuple *tuple = [RACTuple tupleWithObjects:[NSNumber numberWithBool:YES], nil];
                    [subscriber sendNext:tuple];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _codeCommand;
}

-(RACCommand *)registerCommand{
    if(!_registerCommand){
        _registerCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            NSDictionary *params = @{@"password":self.password,@"phoneOrEmail":self.phone,@"code":self.code};
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_PHONE_OR_EMAIL_REGISTER_URL];
            @weakify(self);
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                if(NULLString(self.phone)){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_PHONE_OR_EMAIL_EMPTY]]];
                    [subscriber sendCompleted];
                    return nil;
                }
                if(NULLString(self.code)){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_CODE_EMPTY]]];
                    [subscriber sendCompleted];
                    return nil;
                }
                if(NULLString(self.password)){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_PASSWORD_EMPTY]]];
                    [subscriber sendCompleted];
                    return nil;
                }
                if(![self.password isEqualToString:self.rePassword]){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_INCONSISTENT_PASSWORD]]];
                    [subscriber sendCompleted];
                    return nil;
                }
                if(![ZYBasicHelper validateMobile:self.phone] && ![ZYBasicHelper validateEmail:self.phone]){
                    [subscriber sendError:[NSErrorHelper createErrorWithErrorInfo:[self.language getStringByKey:LC_ERROR_PHONE_OR_EMAIL]]];
                    [subscriber sendCompleted];
                    return nil;
                }
                [[AFNetWorkUtils racPOSTWithURL:url params:params] subscribeNext:^(id x) {
                    NSDictionary *responseObject = x;
                    ZYUser *user = [ZYUser modelWithJSON:[responseObject objectForKey:@"result"]];
                    [ZYConfig saveUserInfo:user];
                    RACTuple *tuple = [RACTuple tupleWithObjects:[NSNumber numberWithBool:YES], nil];
                    [subscriber sendNext:tuple];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _registerCommand;
}

@end
